/******************************************************************/
/*                                                                */
/*  Winpooch : Windows Watchdog                                   */
/*  Copyright (C) 2004  Benoit Blanchon                           */
/*                                                                */
/*  This program is free software; you can redistribute it        */
/*  and/or modify it under the terms of the GNU General Public    */
/*  License as published by the Free Software Foundation; either  */
/*  version 2 of the License, or (at your option) any later       */
/*  version.                                                      */
/*                                                                */
/*  This program is distributed in the hope that it will be       */
/*  useful, but WITHOUT ANY WARRANTY; without even the implied    */
/*  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       */
/*  PURPOSE.  See the GNU General Public License for more         */
/*  details.                                                      */
/*                                                                */
/*  You should have received a copy of the GNU General Public     */
/*  License along with this program; if not, write to the Free    */
/*  Software Foundation, Inc.,                                    */
/*  675 Mass Ave, Cambridge, MA 02139, USA.                       */
/*                                                                */
/******************************************************************/

// standards headers
#include <shlobj.h>
#include <windows.h>

// project's headers
#include "DirWatcher.h"
#include "Language.h"
#include "Log.h"
#include "MainWindow.h"
#include "RegWatcher.h"
#include "Version.h"

// check if we use unicode
#ifndef UNICODE
#warning Compiling without using UNICODE
#endif
#ifndef _UNICODE
#warning Compiling without using _UNICODE
#endif


#define DIRWATCHERS  3

#define REGWATCHERS  2



int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
		    LPSTR lpCmdLine, int nCmdShow)
{
  int		i ;
  MSG		msg ;
  HWND		hwnd ;
  HANDLE	hMutex ;
  HDIRWATCHER	aDirWatch[DIRWATCHERS] ;
  HREGWATCHER	aRegWatch[REGWATCHERS] ;
  OSVERSIONINFO	osInfo = { sizeof(OSVERSIONINFO) } ;
  
  // check OS version, should be NT
  GetVersionEx (&osInfo) ;
  if( osInfo.dwPlatformId != VER_PLATFORM_WIN32_NT )
    {
      MessageBox (NULL, TEXT("This program requires Microsoft Windows NT, 2000, XP or higher"), 
		  TEXT(APPLICATION_NAME), MB_ICONERROR) ;
      return 1 ;
    }

  // init language module
  if( ! Language_Init() )
    {
      MessageBox (NULL, TEXT("Failed to initialize language module"),
		  TEXT(APPLICATION_NAME), MB_ICONERROR) ;
      goto failed ;
    } 

  // set mutex to avoid multiple instance
  hMutex = CreateMutex (NULL, TRUE, TEXT(APPLICATION_NAME)) ;

  // an instance is already running ?
  if( GetLastError()!=0 )
    {
      MessageBox (NULL, STR(_ALREADY_RUNNING), 
		  TEXT(APPLICATION_NAME), MB_ICONERROR) ;
      goto failed ;
    }
  
  // init log module
  if( ! Log_Init(APPLICATION_NAME TEXT(".log")) )
    {
      MessageBox (NULL, TEXT("Failed to initialize log module"),
		  TEXT(APPLICATION_NAME), MB_ICONERROR) ;
      goto failed ;
    } 

  // register main window's class
  if( ! MainWnd_RegisterClass (hInstance) )
    {
      MessageBox (NULL, TEXT("Failed to register window class"),
		  TEXT(APPLICATION_NAME), MB_ICONERROR) ;
      goto failed ;
    } 

  // create main window
  hwnd = MainWnd_CreateWindow (hInstance) ;

  // add watchers
  aDirWatch[0] = DirWatcher_NewByCSIDL (hwnd, CSIDL_SYSTEM, FALSE) ;
  aDirWatch[1] = DirWatcher_NewByCSIDL (hwnd, CSIDL_COMMON_STARTUP, FALSE) ;
  aDirWatch[2] = DirWatcher_NewByCSIDL (hwnd, CSIDL_STARTMENU, TRUE) ;

  // add reg watchers
  aRegWatch[0] = RegWatcher_New (hwnd, TEXT("HKLM\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"), TRUE) ;
  aRegWatch[1] = RegWatcher_New (hwnd, TEXT("HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"), TRUE) ;

  // log message
  Log_Print (APPLICATION_NAME TEXT(" started")) ;

  // message pump
  while (GetMessage (&msg, NULL, 0, 0))
    {
      TranslateMessage (&msg) ;
      DispatchMessage (&msg) ;
    }

  for( i=0 ; i<REGWATCHERS ; i++ )
    RegWatcher_Delete (aRegWatch[i]) ;
  for( i=0 ; i<DIRWATCHERS ; i++ )
    DirWatcher_Delete (aDirWatch[i]) ;
   
  // log message
  Log_Print (APPLICATION_NAME TEXT(" stopped")) ;

 failed :

  Language_Uninit () ;
  Log_Uninit () ;
  CloseHandle (hMutex) ;
  
  return msg.wParam ;
}
